package org.zebias.udpnettydemo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.zebias.udpnettydemo.client.UdpClient;
import org.zebias.udpnettydemo.server.UdpServer;

import java.net.InetSocketAddress;
import java.util.List;


@SpringBootApplication
public class UdpnettydemoApplication {

    public static void main(String[] args) {
//        SpringApplication.run(UdpnettydemoApplication.class, args);
//        System.out.println(args);
        // 单播
        if ("single".equalsIgnoreCase(args[0])) {
            if ("client".equalsIgnoreCase(args[1])) {
                InetSocketAddress inetSocketAddress = new InetSocketAddress(args[2], Integer.parseInt(args[3]));
                // 客户端
                try {
                    new UdpClient(inetSocketAddress).runSingle();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                // 服务端
            }else{
                try {
                    new UdpServer().runSingle(Integer.parseInt(args[2]));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        if("multicast".equalsIgnoreCase(args[0])){
            InetSocketAddress inetSocketAddress = new InetSocketAddress(args[2], Integer.parseInt(args[3]));
            if ("client".equalsIgnoreCase(args[1])) {
                // 客户端
                try {
                    new UdpClient(inetSocketAddress).runMulticast(args[4]);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }else{
                try {
                    new UdpServer(inetSocketAddress).runMulticast(args[4]);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        if("broadcast".equalsIgnoreCase(args[0])){
            if ("client".equalsIgnoreCase(args[1])) {
                // 客户端
                InetSocketAddress inetSocketAddress = new InetSocketAddress(args[2], Integer.parseInt(args[3]));
                try {
                    new UdpClient(inetSocketAddress).runBoradCast();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }else{
                InetSocketAddress inetSocketAddress = new InetSocketAddress(Integer.parseInt(args[2]));
                try {
                    new UdpServer(inetSocketAddress).runBroadCast();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        if("help".equalsIgnoreCase(args[0])){
            System.out.println("-----------------------------------UDP 单播---------------------------------------");
            System.out.println("Param: single  client  ip port");
            System.out.println("Simple: java -jar udpnettydemo-0.0.1-SNAPSHOT.jar single client 192.168.188.40 9000");
            System.out.println("Param: single  server  port");
            System.out.println("Simple: java -jar udpnettydemo-0.0.1-SNAPSHOT.jar single server 9000");
            System.out.println("------------------------------------UDP 组(多)播---------------------------------------");
            System.out.println("Param: multicast  client  ip port net");
            System.out.println("Simple: java -jar udpnettydemo-0.0.1-SNAPSHOT.jar multicast client 239.2.1.1 9001 eth2");
            System.out.println("Param: multicast  server  ip  port net");
            System.out.println("Simple: java -jar udpnettydemo-0.0.1-SNAPSHOT.jar multicast server 239.2.1.1 9001 eth2");
            System.out.println("-------------------------------------UDP 广播-----------------------------------");
            System.out.println("Param: broadcast  client  ip  port");
            System.out.println("Simple: java -jar udpnettydemo-0.0.1-SNAPSHOT.jar broadcast client 192.168.188.255 9002");
            System.out.println("Param: broadcast  server  port");
            System.out.println("Simple: java -jar udpnettydemo-0.0.1-SNAPSHOT.jar broadcast server  9002");
            System.out.println("-----------------------------------------------------------------------------------");
            System.out.println("Param: help");
        }
        // 组播
    }

}
